package com.situ.controller;



import com.situ.bean.Product;
import com.situ.service.impl.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/products")
public class AdminProductController {

    @Autowired
    private ProductService productService;

    // 获取所有商品
    @GetMapping
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }

    // 根据ID获取商品
    @GetMapping("/{id}")
    public Product getProductById(@PathVariable int id) {
        return productService.getProductById(id);
    }

    // 添加商品
    @PostMapping
    public String addProduct(@RequestBody Product product) {
        productService.addProduct(product);
        return "商品添加成功";
    }

    // 更新商品
    @PutMapping("/{id}")
    public String updateProduct(@PathVariable int id, @RequestBody Product product) {
        product.setId(id); // 确保ID一致
        productService.updateProduct(product);
        return "商品更新成功";
    }

    // 删除商品
    @DeleteMapping("/{id}")
    public String deleteProduct(@PathVariable int id) {
        productService.deleteProduct(id);
        return "商品删除成功";
    }
}